<template>
  <div class="confirm" v-show="visible" >
    <div class="body">
      <header>
        <h4>{{ title }}</h4>
        <span class="close" @click="close">X</span>
      </header>
      <!-- 中间 -->
      <section>
        <slot :person="{ name: '何世纪', age: 23 }"></slot>
      </section>
      <!-- 底部 -->
      <footer>
        <slot name="footer">
          <button class="btn" @click="oncancel">取消</button>
          <button class="btn primary" @click="onfinish">确定</button>
        </slot>
      </footer>
    </div>
    <!-- 头部 -->
  </div>
</template>

<script>
export default {
  name: 'Confirm',
  props: {
    title: {
      type: String,
      default: '提示'
    },
    visible: {
      type: Boolean,
      default: false
    },
    oncancel: {
      type: Function,
    },
    onfinish: {
      type: Function,
    }
  },
  data() {
    return {

    }
  },
  methods: {
    close() {
      this.$emit('update:visible', false)
    }
  },
  mounted() {
    // console.log(this.$slots)
    // console.log('mounted')
  },
  watch: {
    visible: {
      handler(newValue){
        if(newValue) {
          document.documentElement.style.overflow = "hidden"
        } else {
          document.documentElement.style.overflow = "auto"
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>
@keyframes fadeIn {
  0%   {background-color: rgba(0, 0, 0, .2);}
  25%  {background-color: rgba(0, 0, 0, .3);}
  50%  {background-color: rgba(0, 0, 0, .4);}
  75%  {background-color: rgba(0, 0, 0, .5);}
  100% {background-color: rgba(0, 0, 0, .6);}
}
@keyframes fadeOut {
  0%   {background-color: rgba(0, 0, 0, .6);}
  25%  {background-color: rgba(0, 0, 0, .5);}
  50%  {background-color: rgba(0, 0, 0, .4);}
  75%  {background-color: rgba(0, 0, 0, .3);}
  100% {background-color: rgba(0, 0, 0, .2);}
}

.confirm {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  // background-color: rgba(0, 0, 0, .6);
  animation: fadeIn .3s forwards;
  z-index: 1000;
  .body {
    width: 600px;
    margin: 120px auto;
    padding: 15px;
    background-color: #fff;
    border-radius: 3px;
    transition: .5s;
    header {
      display: flex;
      justify-content: space-between;
      h4 {
        font-size: 18px;
        font-weight: normal;
      }
      .close:hover {
        color: #409eff;
        cursor: pointer;
      }
    }
    section {
      padding: 30px 0;
      color: #606266;
      font-size: 14px;
      word-break: break-all;
    }
    footer {
      text-align: right;

      button {
        display: inline-block;
        line-height: 1;
        white-space: nowrap;
        cursor: pointer;
        background: #fff;
        border: 1px solid #dcdfe6;
        text-align: center;
        box-sizing: border-box;
        outline: none;
        margin: 0 10px;
        transition: .1s;
        font-weight: 500;
        -moz-user-select: none;
        -webkit-user-select: none;
        -ms-user-select: none;
        padding: 12px 20px;
        font-size: 14px;
        border-radius: 4px;
      }

      .btn:hover {
        opacity: .8;
      }

      .primary {
        color: #fff;
        background-color: #409eff;
        border-color: #409eff;
      }
    }
  }
}
</style>